package com.hl.ruoyicsmshop.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hl.ruoyicsmshop.mapper.DoorsillActivityMapper;
import com.hl.ruoyicsmshop.mapper.DoorsillActivityGoodsMapper;
import com.hl.ruoyicsmshop.domain.DoorsillActivity;
import com.hl.ruoyicsmshop.domain.DoorsillActivityGoods;
import com.hl.ruoyicsmshop.service.IDoorsillActivityService;

/**
 * 满额活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class DoorsillActivityServiceImpl implements IDoorsillActivityService 
{
    @Autowired
    private DoorsillActivityMapper doorsillActivityMapper;

    @Autowired
    private DoorsillActivityGoodsMapper doorsillActivityGoodsMapper;

    /**
     * 查询满额活动
     * 
     * @param daId 满额活动主键
     * @return 满额活动
     */
    @Override
    public DoorsillActivity selectDoorsillActivityByDaId(Integer daId)
    {
        return doorsillActivityMapper.selectDoorsillActivityByDaId(daId);
    }

    /**
     * 查询满额活动列表
     * 
     * @param doorsillActivity 满额活动
     * @return 满额活动
     */
    @Override
    public List<DoorsillActivity> selectDoorsillActivityList(DoorsillActivity doorsillActivity)
    {
        return doorsillActivityMapper.selectDoorsillActivityList(doorsillActivity);
    }

    /**
     * 查询进行中的满额活动
     * 
     * @return 满额活动集合
     */
    @Override
    public List<DoorsillActivity> selectActiveDoorsillActivityList()
    {
        return doorsillActivityMapper.selectActiveDoorsillActivityList();
    }

    /**
     * 根据商家ID查询满额活动
     * 
     * @param businessId 商家ID
     * @return 满额活动集合
     */
    @Override
    public List<DoorsillActivity> selectDoorsillActivityByBusinessId(Integer businessId)
    {
        return doorsillActivityMapper.selectDoorsillActivityByBusinessId(businessId);
    }

    /**
     * 新增满额活动
     * 
     * @param doorsillActivity 满额活动
     * @param goodsList 活动商品列表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDoorsillActivity(DoorsillActivity doorsillActivity, List<DoorsillActivityGoods> goodsList)
    {
        // 设置活动状态
        Date now = new Date();
        if (doorsillActivity.getDaStartTime().after(now)) {
            doorsillActivity.setDaStatus(0); // 未开始
        } else if (doorsillActivity.getDaEndTime().before(now)) {
            doorsillActivity.setDaStatus(2); // 已结束
        } else {
            doorsillActivity.setDaStatus(1); // 已开始
        }

        int rows = doorsillActivityMapper.insertDoorsillActivity(doorsillActivity);
        
        // 如果有商品列表，则插入活动商品关联
        if (goodsList != null && !goodsList.isEmpty() && doorsillActivity.getDaRange() == 1) {
            for (DoorsillActivityGoods goods : goodsList) {
                goods.setDaId(doorsillActivity.getDaId());
            }
            doorsillActivityGoodsMapper.batchInsertDoorsillActivityGoods(goodsList);
        }
        
        return rows;
    }

    /**
     * 修改满额活动
     * 
     * @param doorsillActivity 满额活动
     * @param goodsList 活动商品列表
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDoorsillActivity(DoorsillActivity doorsillActivity, List<DoorsillActivityGoods> goodsList)
    {
        // 更新活动状态
        Date now = new Date();
        if (doorsillActivity.getDaStartTime().after(now)) {
            doorsillActivity.setDaStatus(0); // 未开始
        } else if (doorsillActivity.getDaEndTime().before(now)) {
            doorsillActivity.setDaStatus(2); // 已结束
        } else {
            doorsillActivity.setDaStatus(1); // 已开始
        }

        int rows = doorsillActivityMapper.updateDoorsillActivity(doorsillActivity);
        
        // 如果是指定商品活动，则更新商品关联
        if (doorsillActivity.getDaRange() == 1) {
            // 先删除原有的商品关联
            doorsillActivityGoodsMapper.deleteDoorsillActivityGoodsByDaId(doorsillActivity.getDaId());
            
            // 插入新的商品关联
            if (goodsList != null && !goodsList.isEmpty()) {
                for (DoorsillActivityGoods goods : goodsList) {
                    goods.setDaId(doorsillActivity.getDaId());
                }
                doorsillActivityGoodsMapper.batchInsertDoorsillActivityGoods(goodsList);
            }
        }
        
        return rows;
    }

    /**
     * 删除满额活动信息
     * 
     * @param daId 满额活动主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDoorsillActivityByDaId(Integer daId)
    {
        // 先删除活动商品关联
        doorsillActivityGoodsMapper.deleteDoorsillActivityGoodsByDaId(daId);
        // 再删除活动
        return doorsillActivityMapper.deleteDoorsillActivityByDaId(daId);
    }

    /**
     * 批量删除满额活动
     * 
     * @param daIds 需要删除的满额活动主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDoorsillActivityByDaIds(Integer[] daIds)
    {
        // 先删除活动商品关联
        doorsillActivityGoodsMapper.deleteDoorsillActivityGoodsByDaIds(daIds);
        // 再删除活动
        return doorsillActivityMapper.deleteDoorsillActivityByDaIds(daIds);
    }

    /**
     * 更新活动状态
     * 
     * @param daId 满额活动主键
     * @param daStatus 活动状态
     * @return 结果
     */
    @Override
    public int updateDoorsillActivityStatus(Integer daId, Integer daStatus)
    {
        return doorsillActivityMapper.updateDoorsillActivityStatus(daId, daStatus);
    }

    /**
     * 查询满额活动商品列表
     * 
     * @param daId 满额活动ID
     * @return 活动商品集合
     */
    @Override
    public List<DoorsillActivityGoods> selectDoorsillActivityGoodsByDaId(Integer daId)
    {
        return doorsillActivityGoodsMapper.selectDoorsillActivityGoodsByDaId(daId);
    }

    /**
     * 根据商品ID查询满额活动
     * 
     * @param goodsId 商品ID
     * @return 满额活动商品集合
     */
    @Override
    public List<DoorsillActivityGoods> selectDoorsillActivityGoodsByGoodsId(Integer goodsId)
    {
        return doorsillActivityGoodsMapper.selectDoorsillActivityGoodsByGoodsId(goodsId);
    }

    /**
     * 检查商品是否参与满额活动
     * 
     * @param goodsId 商品ID
     * @return 是否参与活动
     */
    @Override
    public boolean checkGoodsInDoorsillActivity(Integer goodsId)
    {
        List<DoorsillActivityGoods> goodsList = doorsillActivityGoodsMapper.selectDoorsillActivityGoodsByGoodsId(goodsId);
        return goodsList != null && !goodsList.isEmpty();
    }

    /**
     * 计算满额优惠金额
     * 
     * @param totalAmount 订单总金额
     * @param goodsIds 商品ID列表
     * @return 优惠金额
     */
    @Override
    public Double calculateDoorsillDiscount(Double totalAmount, List<Integer> goodsIds)
    {
        if (totalAmount == null || totalAmount <= 0) {
            return 0.0;
        }

        // 查询所有进行中的满额活动
        List<DoorsillActivity> activeActivities = doorsillActivityMapper.selectActiveDoorsillActivityList();
        
        Double maxDiscount = 0.0;
        
        for (DoorsillActivity activity : activeActivities) {
            // 如果是全品类活动，直接计算
            if (activity.getDaRange() == 0) {
                if (totalAmount >= activity.getDaDoorsill()) {
                    maxDiscount = Math.max(maxDiscount, activity.getDaMoney());
                }
            } 
            // 如果是指定商品活动，需要检查商品是否参与
            else if (activity.getDaRange() == 1 && goodsIds != null && !goodsIds.isEmpty()) {
                boolean hasParticipatingGoods = false;
                for (Integer goodsId : goodsIds) {
                    if (checkGoodsInDoorsillActivity(goodsId)) {
                        hasParticipatingGoods = true;
                        break;
                    }
                }
                
                if (hasParticipatingGoods && totalAmount >= activity.getDaDoorsill()) {
                    maxDiscount = Math.max(maxDiscount, activity.getDaMoney());
                }
            }
        }
        
        return maxDiscount;
    }
}
